package com.kylinhunter.ui.common;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.kylinhunter.ch.language.LangChangeListener;
import com.kylinhunter.ch.language.ResourceManager;
import com.kylinhunter.ch.plugin.knowledgebase.service.imp.KBServiceImp;

public class DesktopPanel  extends  DesktopPanel_UI implements KHDesktop, LangChangeListener{
	private static final Log log = LogFactory.getLog(DesktopPanel.class);
	private Map<String, DeskTopPanelInternalFrame> windows = new HashMap<String, DeskTopPanelInternalFrame>();
	private Map<String, DesktopPanelButton> buttons = new HashMap<String, DesktopPanelButton>();
	private DeskTopInternalFrameAdapter dia=new DeskTopInternalFrameAdapter(this);
	public DesktopPanel() {
		super(); 
		this.getBottomscrollPane().setVisible(false);
	}
	@Override
	public boolean addWindow(DeskTopPanelInternalFrame window) {
		
		if(windows.containsKey(window.getWindows_id()))return false;
		window.addInternalFrameListener(dia);
		log.debug("add delete windowListener!");
		// TODO Auto-generated method stub
		this.getBottomscrollPane().setVisible(true);
		this.windows.put(window.getWindows_id(), window);

		window.setPreferredSize(new java.awt.Dimension(400, 300));
		window.setIconifiable(true);
		window.setMaximizable(true);
		window.setResizable(true);
		window.setClosable(true);
		
	 
 

		DesktopPanelButton button = new DesktopPanelButton();
		button.setText(window.getTitle());
		button.setWindows_id(window.getWindows_id());
		button.setActionCommand(window.getWindows_id());
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String window_id = e.getActionCommand();
				showWindow(window_id);
			}

		});
		this.buttons.put(window.getWindows_id(), button);
		
		this.getBottomPanel().add(button);
		this.getDefaultJavaDesktopPane().add(window,javax.swing.JLayeredPane.DEFAULT_LAYER);
		try {
			window.setSelected(true);
			window.setMaximum(true);
//			window.revalidate();
			window.repaint();
//			this.getDefaultJavaDesktopPane().revalidate();
//			this.getDefaultJavaDesktopPane().repaint();
			window.setVisible(true);
			
		} catch (PropertyVetoException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		return false;
	}


 
	public boolean delWindow(String window_id) {
		// TODO Auto-generated method stub
		DeskTopPanelInternalFrame window=this.windows.get( window_id);
		DesktopPanelButton button=this.buttons.get(window_id);
		this.getDefaultJavaDesktopPane().remove(window);
		this.getBottomPanel().remove(button);		
		this.windows.remove(window_id);
		this.buttons.remove(window_id); 
		this.getBottomPanel().revalidate();
		this.getBottomPanel().repaint();
        window.removeInternalFrameListener(dia);
        log.debug("remove delete windowListener!");
        if(windows.size()==0){
        	this.getBottomscrollPane().setVisible(false); 
        	this.revalidate();
        	this.repaint();
        } 
		return false;
	}
	@Override
	public boolean showWindow(String window_id) {
		DeskTopPanelInternalFrame window = this.windows.get(window_id);
		try {
			window.setSelected(true);
			window.setVisible(true);
			if(window.isIcon()){
				log.debug("isIcon");
				window.setIcon(false);

			}else{
				log.debug("is not Icon");
				window.setMaximum(true);
			}

		} catch (PropertyVetoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;

	}
 
	@Override
	public boolean hasWindow(String windowId) {
		// TODO Auto-generated method stub
		if(this.windows.containsKey(windowId)){
			return true;
		}
		return false;
	}
	@Override
	public void updateResource(ResourceManager resourceManager) {
		// TODO Auto-generated method stub
		for(DesktopPanelButton b:buttons.values()){
			DeskTopPanelInternalFrame dif=windows.get(b.getWindows_id());
			b.setText(dif.getTitle());
			
		}
	} 
}
